package common.system.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import system.util.SystemCache;

import com.opensymphony.xwork2.ActionSupport;
import common.customer.bean.Customer;
import common.system.bean.City;
import common.system.service.CityService;

public class CityAction extends ActionSupport {
	private CityService cityService;
	private static final long serialVersionUID = 1L;
	private int cityId;
	// 城市名
	private String cityName;
	// 城市所在区域名称-华中、华北等
	private String areaName;
	// 城市区域名称缩写
	private String areaNameDesc;
	// 城市首字母缩写
	private String shortName;

	private String resultStr;
	private String resultCode;
	private List<City> list;
	private int currentCity;
	/**
	 * find all city
	 * @return
	 */
	public String findAllCity() {
		if(SystemCache.cityCache.size()>0){
			this.list = SystemCache.cityCache;
		}else{
			this.list= this.cityService.getAllCity();
			if(list!=null){
				SystemCache.cityCache.addAll(list);
			}
		}
		Object o = ServletActionContext .getRequest().getSession().getAttribute("city");
		if(o==null){
			Object c = ServletActionContext .getRequest().getSession().getAttribute("user");
			if(c!=null){
				Customer custumer = (Customer)c;
				currentCity = custumer.getCity().getId();
			}else{				
				currentCity=1;
			}
		}else{
			currentCity = ((City)o).getId();
		}
		resultCode="1";
		return SUCCESS;
	}
	/**
	 * add a city
	 * @return
	 */
	public String addCity() {
		//City city = new City();
		//city.setCityName(cityName);
		if(cityName==null || "".equals(cityName.trim()) || shortName==null || "".equals(shortName.trim())){
			resultStr="城市名称或者城市缩写不能为空";
			resultCode="0";
		}else{
			List<City> cities = cityService.getCityByName(cityName.trim());
			if(cities!=null && cities.size()>0){
				City city = cities.get(0);
				city.setCityName(cityName);
				city.setAreaName(areaName);
				city.setAreaNameDesc(areaNameDesc);
				city.setShortName(shortName);
				boolean result = cityService.updateCity(city);
				if(result){
					resultStr = "更新城市成功";
					resultCode="1";
				}else{
					resultCode="0";
				}
			}else{
				City city = new City();
				city.setCityName(cityName);
				city.setAreaName(areaName);
				city.setAreaNameDesc(areaNameDesc);
				city.setShortName(shortName);
				boolean result = cityService.addCity(city);
				if(result){
					resultStr = "添加城市成功";
					//城市加到缓存里
					list = this.cityService.getAllCity();
					if(list!=null){
						SystemCache.cityCache.clear();
						SystemCache.cityCache.addAll(list);
					}
					resultCode="1";
				}else{
					resultCode="0";
				}
			}
		}
		return SUCCESS;
	}
	/**
	 * 更新城市
	 * @return
	 */
	public String updateCity(){
		if(cityId==0){
			resultStr = "修改城市失败，传递的ID值为空";
			resultCode="0";
		}else{
			City city = this.cityService.getCityByID(cityId);
			city.setAreaName(areaName);
			city.setCityName(cityName);
			city.setAreaNameDesc(areaNameDesc);
			city.setShortName(shortName);
			boolean result = this.cityService.updateCity(city);
			if(!result){
				resultStr="添加城市失败";
				resultCode="0";
			}else{
				resultCode="1";
			}
		}
		return SUCCESS;
	}
	public String changeCity(){
		if(cityId==0){
			resultStr = "修改城市失败，页面发生错误，请联系管理员";
			resultCode="0";
			return SUCCESS;
		}else{			
			if(SystemCache.cityCache.size()>0){
				for(City c:SystemCache.cityCache){
					if(c.getId()==cityId){
						ServletActionContext.getRequest().getSession().setAttribute("city", c);
						resultCode="1";
						return SUCCESS;
					}
				}
			}else{
				SystemCache.cityCache.addAll(this.cityService.getAllCity());
				for(City c:SystemCache.cityCache){
					if(c.getId()==cityId){
						ServletActionContext.getRequest().getSession().setAttribute("city", c);
						resultCode="1";
						return SUCCESS;
					}
				}
			}
			resultStr = "修改城市失败,没找到对应的城市，请联系管理员";
			resultCode="0";
			return SUCCESS;
		}
	}
	
	
	//==================getter setter=======================方法
	
	public String getCityName() {
		return cityName;
	}
	public int getCurrentCity() {
		return currentCity;
	}
	public void setCurrentCity(int currentCity) {
		this.currentCity = currentCity;
	}
	public String getResultCode() {
		return resultCode;
	}
	public void setResultCode(String resultCode) {
		this.resultCode = resultCode;
	}
	public int getCityId() {
		return cityId;
	}
	public void setCityId(int cityId) {
		this.cityId = cityId;
	}
	public void setCityService(CityService cityService) {
		this.cityService = cityService;
	}
	public List<City> getList() {
		return list;
	}
	public void setList(List<City> list) {
		this.list = list;
	}
	public void setCityName(String cityName) {
		this.cityName = cityName;
	}
	public String getAreaName() {
		return areaName;
	}
	public void setAreaName(String areaName) {
		this.areaName = areaName;
	}
	public String getAreaNameDesc() {
		return areaNameDesc;
	}
	public void setAreaNameDesc(String areaNameDesc) {
		this.areaNameDesc = areaNameDesc;
	}
	public String getShortName() {
		return shortName;
	}
	public void setShortName(String shortName) {
		this.shortName = shortName;
	}
	public String getResultStr() {
		return resultStr;
	}
	public void setResultStr(String resultStr) {
		this.resultStr = resultStr;
	}
	
}
